package com.itheima.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.AddressBook;
import com.itheima.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * reggie-com.itheima.Controller
 * Administrator
 * 2023-03-12
 * 21:35
 */
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    //新增地址
    @PostMapping
    public R save (@RequestBody AddressBook addressBook){
        Long currentId = BaseContext.getCurrentId();
        addressBook.setUserId(currentId);
        boolean save = addressBookService.save(addressBook);
        if (save){
            return  R.success("保存地址成功");
        }
        return R.error("保存地址失败");
    }

    //设置默认地址
    @PutMapping("/default")
    public R  setDefaultAddress(@RequestBody AddressBook addressBook){
        //先将该用户所有地址的is_default更新为0 ,
        Long id = BaseContext.getCurrentId();
        LambdaUpdateWrapper<AddressBook> lqw = new LambdaUpdateWrapper<>();
        lqw.set(AddressBook::getIsDefault,0);
        lqw.eq(AddressBook::getUserId,id);
        boolean update = addressBookService.update(lqw);
        if (update){
            //然后将当前的设置的默认地址的is_default设置为1
            addressBook.setIsDefault(1);
            boolean b = addressBookService.updateById(addressBook);
            if (b){
                return  R.success("设置成功");
            }
        }
        return R.error("设置失败");
    }

    //根据ID查询地址

    @GetMapping("/{id}")
    public  R selectAddressBuId(@PathVariable Long id){
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getId,id);
        AddressBook one = addressBookService.getOne(lqw);
        if (one!=null){
            return R.success(one);
        }
        return R.error("查询失败");
    }


    //查询默认地址
    @GetMapping("/default")
    public R selectDefaultAddress(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId,currentId);
        lqw.eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(lqw);
        if (one!=null){
            return R.success(one);
        }
        return R.error("查询失败");
    }


    //查询指定用户的全部地址
    @GetMapping("/list")
    public R selectAllAddress(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId,currentId);
        List<AddressBook> list = addressBookService.list(lqw);
        if (list.size()!=0){
            return R.success(list);
        }
        return R.error("查询失败");
    }

    //删除地址【可能是多个】
    @DeleteMapping
    public R deleteAddress(@RequestParam List<Long> ids){
        boolean remove = addressBookService.removeByIds(ids);
        if (remove){
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    //修改地址
    @PutMapping
    public R updateAddress(@RequestBody AddressBook addressBook){
        boolean b = addressBookService.updateById(addressBook);
        if (b){
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }






}
